home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Commun⁄Network / Telnet 2.5.src.ThinkC / source / prefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-05  |  13.0 KB  |  367 lines  |  [TEXT/MPS ]

  1. #ifdef lint
  2. static char *SCCSid = "%W%    (NCSA)    %G%";
  3. #endif
  4. /*
  5. *    prefs.c
  6. *     by Gaige B. Paulsen
  7. ****************************************************************************
  8. *    Part of NCSA Telnet for the Macintosh                                   *
  9. *                                                                          *
  10. *      Uses    :                                                               *
  11. *      TCP/IP kernel for NCSA Telnet                                       *
  12. *      by Tim Krauskopf                                                    *
  13. *       with Macintosh code by Gaige B. Paulsen                                 *
  14. *                                                                          *
  15. *      National Center for Supercomputing Applications                     *
  16. *      152 Computing Applications Building                                 *
  17. *      605 E. Springfield Ave.                                             *
  18. *      Champaign, IL  61820                                                *
  19. *                                                                          *
  20. *                                                                          *
  21. ****************************************************************************
  22. *
  23. *    Preferences reading,writing, and manipulating code.
  24. *
  25. *    Called by:
  26. *        menu.c
  27. *        maclook.c
  28. */
  29.  
  30. #include <StdIO.h>
  31. #include <String.h>
  32.  
  33. #include <Controls.h>
  34. #include <Dialogs.h>
  35. #include <Memory.h>
  36. #include <Resources.h>
  37.  
  38. #include "prefs.h"
  39. #include "configrec.h"
  40. #include "maclook.h"
  41. #include "menu.h"
  42. #include "event.h"
  43.  
  44. #ifdef MPW
  45. #include "mpw.h"
  46. #endif MPW
  47.  
  48. #define    DLOGOk        1
  49. #define DLOGCancel    2
  50.  
  51. #define    PrefDLOG    700        /* The Preferences Dialog        ---------------- */
  52. #define PrefOutline 3        /*   outline for OK */
  53. #define PrefCTt        5        /*   Copy table threshold */
  54. #define PrefCMDkey    6        /*   Command keys */
  55. #define PrefSave    7        /*     Save the Preferences */
  56. #define PrefBinary    8        /*   MacBinary active by default */
  57. #define PrefBReset    9        /*   Reset MacBinary to default at Beginning of FTP? */
  58. #define PrefOPTkey  10        /*     Remap option key to control */
  59. #define PrefTitle    11        /*      This is just a title */
  60. #define PrefDClose    12        /*      Don't close the window on "Close" */
  61. #define PrefTMap    13        /*     Shall we map the tilde? */
  62. #define PrefStag    14        /*   Staggered Windows? */
  63. #define PrefBlink   15        /*   BYU 2.4.11 - Blink the cursor? */
  64. #define PrefBlockCursor           16    /* BYU 2.4.11 - Display a block cursor? */
  65. #define PrefUnderscoreCursor    17    /* BYU 2.4.11 - Display an underscore cursor? */
  66. #define PrefVerticalCursor        18    /* BYU 2.4.11 - Display a vertical line cursor? */
  67. #define PrefsysKCHR             19    /*   BYU 2.4.18 - Use system KCHR? */
  68.  
  69. extern SysEnvRec theWorld;        /* BYU 2.4.18 - System Environment record */
  70.  
  71. extern int whichMenus;        /* From Menu.c - which menu set is active */
  72. extern int MacBinary;        /* From Menu.c - is MacBinary active ? */
  73. extern int SettingsFile;    /* From Maclook.c - where is our resource file */
  74. extern int mapTilde;        /* From Event.c - are we currently mapping the tilde */
  75. extern short cursorBlink;    /* BYU 2.4.11 - From Event.c, are we blinking? */
  76. extern short cursorType;    /* BYU 2.4.11 - From Event.c, what kind of cursor? */
  77.  
  78. pascal void
  79.     VersionNumber(),        /* Version number display UI code */
  80.     OutlineItem();            /* Outline Item UI code */
  81. pascal int
  82.     modalProc();            /* Keep-alive proc */
  83.  
  84. /*
  85.  *  SavePrefs()    - puts the preferences record into the current resource file
  86.  *
  87.  */
  88. void SavePrefs
  89.   (
  90.     void
  91.   )
  92. {
  93.     NCSAPrefs *ppref, **hpref;
  94.  
  95.     if (SettingsFile == -1)
  96.         return;                /* Can't get it if there is no settings file */
  97.  
  98.     UseResFile(SettingsFile);
  99.  
  100.     hpref = (NCSAPrefs **) Get1Resource('NPRF',1);
  101.  
  102.     if (hpref==0L) {
  103.         hpref = (NCSAPrefs **) NewHandle( (long) sizeof(NCSAPrefs));    /* BYU LSC */
  104.         AddResource((Handle) hpref, 'NPRF', 1, "\pPreferences");
  105.         }
  106.     else        /* The current handle may be too small, so fix it */
  107.         if ( ((int) GetHandleSize((Handle) hpref)) < sizeof(NCSAPrefs))
  108.             ReallocHandle((Handle) hpref, (long) sizeof(NCSAPrefs));
  109.  
  110.     HLock((Handle) hpref);
  111.         ppref = *hpref;
  112.         *ppref = Prefs;
  113.     HUnlock((Handle) hpref);
  114.  
  115.     ChangedResource((Handle) hpref);
  116.     UpdateResFile(SettingsFile);
  117. }
  118.  
  119. void PromptPrefs
  120.   (
  121.     void
  122.   )
  123. {
  124.     DialogPtr dtemp;
  125.     short dItem;
  126.     int tildeMap,dontclose, optKeys, cmdKeys,tempi,stag,sysKCHR,blink,curs;    /* BYU 2.4.18 */
  127.     Rect dBox;
  128.     Str255 temp, *tempp;
  129.     Handle CTtString, saveButton, CMDkeyCtrl, OPTkeyCtrl,
  130.             DCloseCtrl, MacBResetCtrl, MacBinaryCtrl, TMapCtrl, stagHndl,    /* BYU 2.4.11 */
  131.             sysKCHRCtrl,blinkCtrl, curs1Ctrl, curs2Ctrl, curs3Ctrl;            /* BYU 2.4.18 */
  132.     int MacBReset;
  133.  
  134.     tempp = (Str255 *) &temp;
  135.  
  136.     dtemp=GetNewDialog( PrefDLOG, (Ptr) 0L, (WindowPtr) -1L);    /* Bring on the box */
  137.     UItemAssign( dtemp, PrefOutline , (int (*)()) OutlineItem);
  138.  
  139.     if (SettingsFile == -1) {
  140.         GetDItem( dtemp, PrefSave, &dItem, &saveButton, &dBox);
  141.         HiliteControl((ControlHandle) saveButton, 255);
  142.         }
  143.     GetDItem( dtemp, PrefCTt, &dItem, &CTtString, &dBox);
  144.     GetDItem( dtemp, PrefCMDkey, &dItem, &CMDkeyCtrl, &dBox);
  145.     GetDItem( dtemp, PrefBinary, &dItem, &MacBinaryCtrl, &dBox);
  146.     GetDItem( dtemp, PrefBReset, &dItem, &MacBResetCtrl, &dBox);
  147.     GetDItem( dtemp, PrefOPTkey, &dItem, &OPTkeyCtrl, &dBox);
  148.     GetDItem( dtemp, PrefDClose, &dItem, &DCloseCtrl, &dBox);
  149.     GetDItem( dtemp, PrefTMap,   &dItem, &TMapCtrl, &dBox);
  150.     GetDItem( dtemp, PrefStag,   &dItem, &stagHndl, &dBox);
  151.     GetDItem( dtemp, PrefsysKCHR,  &dItem, &sysKCHRCtrl, &dBox);        /* BYU 2.4.18 */
  152.     GetDItem( dtemp, PrefBlink,  &dItem, &blinkCtrl, &dBox);            /* BYU 2.4.11 */
  153.     GetDItem( dtemp, PrefBlockCursor,      &dItem, &curs1Ctrl, &dBox);    /* BYU 2.4.11 */
  154.     GetDItem( dtemp, PrefUnderscoreCursor, &dItem, &curs2Ctrl, &dBox);    /* BYU 2.4.11 */
  155.     GetDItem( dtemp, PrefVerticalCursor,   &dItem, &curs3Ctrl, &dBox);    /* BYU 2.4.11 */
  156.  
  157.     sprintf((char *) tempp, "%d", Prefs.CTthresh);    /* Make it editable */
  158.     c2pstr(tempp);                            /* BYU LSC */
  159.     SetIText( CTtString, tempp );            /* BYU LSC - Install it in the dialog */
  160.     SelIText( dtemp, PrefCTt, 0, 32767);
  161.  
  162.     if (theWorld.systemVersion < 0x0700)                        /* BYU 2.4.18 */
  163.         SetCtlValue((ControlHandle) OPTkeyCtrl, Prefs.optKeys);    /* BYU 2.4.18 - Set up the Check box */
  164.     else {                                                        /* BYU 2.4.18 */
  165.         SetCtlValue((ControlHandle) OPTkeyCtrl, 0);                /* BYU 2.4.18 - Set up the Check box */
  166.         HiliteControl((ControlHandle) OPTkeyCtrl, 255);            /* BYU 2.4.18 */
  167.     }                                                            /* BYU 2.4.18 */
  168.  
  169.     optKeys = Prefs.optKeys;
  170.     SetCtlValue((ControlHandle) CMDkeyCtrl, Prefs.cmdKeys);/* Set up the Check box */
  171.     cmdKeys = Prefs.cmdKeys;
  172.     SetCtlValue((ControlHandle) MacBinaryCtrl, Prefs.MacBinary);/* Set up the Check box */
  173.     MacBinary = Prefs.MacBinary;
  174.     SetCtlValue((ControlHandle) MacBResetCtrl, Prefs.MacBReset);/* Set up the Check box */
  175.     MacBReset = Prefs.MacBReset;
  176.     SetCtlValue((ControlHandle) DCloseCtrl, Prefs.dontclose);/* Set up the Check box */
  177.     dontclose = Prefs.dontclose;
  178.     SetCtlValue((ControlHandle) TMapCtrl, Prefs.mapTilde);/* Set up the Check box */
  179.     tildeMap = Prefs.mapTilde;
  180.     SetCtlValue((ControlHandle) stagHndl, Prefs.stag);/* Set up the Check box */
  181.     stag = Prefs.stag;
  182.     SetCtlValue((ControlHandle) sysKCHRCtrl, Prefs.systemKCHR);    /* BYU 2.4.18 */
  183.     sysKCHR = Prefs.systemKCHR;                                    /* BYU 2.4.18 */
  184.     SetCtlValue((ControlHandle) blinkCtrl, Prefs.cursorBlink);    /* BYU 2.4.11 */
  185.     blink = Prefs.cursorBlink;                                    /* BYU 2.4.11 */
  186.     switch (Prefs.cursorType) {                                    /* BYU 2.4.11 */
  187.         case 0:                                                    /* BYU 2.4.11 */
  188.             SetCtlValue((ControlHandle) curs1Ctrl, 1);            /* BYU 2.4.11 */
  189.             curs = 0;                                            /* BYU 2.4.11 */
  190.             break;                                                /* BYU 2.4.11 */
  191.         case 1:                                                    /* BYU 2.4.11 */
  192.             SetCtlValue((ControlHandle) curs2Ctrl, 1);            /* BYU 2.4.11 */
  193.             curs = 1;                                            /* BYU 2.4.11 */
  194.             break;                                                /* BYU 2.4.11 */
  195.         case 2:                                                    /* BYU 2.4.11 */
  196.             SetCtlValue((ControlHandle) curs3Ctrl, 1);            /* BYU 2.4.11 */
  197.             curs = 2;                                            /* BYU 2.4.11 */
  198.             break;                                                /* BYU 2.4.11 */
  199.         default:                                                /* BYU 2.4.11 */
  200.             SetCtlValue((ControlHandle) curs1Ctrl, 1);            /* BYU 2.4.11 */
  201.             curs = 0;                                            /* BYU 2.4.11 */
  202.             break;                                                /* BYU 2.4.11 */
  203.     }                                                            /* BYU 2.4.11 */
  204.  
  205.  
  206.     dItem=0;                                /* initially no hits */
  207.     while((dItem>2 && dItem !=PrefSave)
  208.                    || (dItem==0)) {                    /* While we are in the loop */
  209.         ModalDialog((ModalFilterProcPtr) modalProc,&dItem); /* and do our duty to the box */
  210.         if (dItem == PrefBinary) {
  211.             MacBinary=!MacBinary;
  212.             SetCtlValue((ControlHandle) MacBinaryCtrl, MacBinary);
  213.             }
  214.         if (dItem == PrefTMap) {
  215.             tildeMap=!tildeMap;
  216.             SetCtlValue((ControlHandle) TMapCtrl, tildeMap);
  217.             }
  218.         if (dItem == PrefDClose) {
  219.             dontclose=!dontclose;
  220.             SetCtlValue((ControlHandle) DCloseCtrl, dontclose);
  221.             }
  222.         if (dItem == PrefBReset) {
  223.             MacBReset=!MacBReset;
  224.             SetCtlValue((ControlHandle) MacBResetCtrl, MacBReset);
  225.             }
  226.         if ((dItem == PrefOPTkey) && (theWorld.systemVersion < 0x0700)) {    /* BYU 2.4.18 */
  227.             optKeys=!optKeys;
  228.             SetCtlValue((ControlHandle) OPTkeyCtrl, optKeys);
  229.             }
  230.         if (dItem == PrefCMDkey) {
  231.             cmdKeys=!cmdKeys;
  232.             SetCtlValue((ControlHandle) CMDkeyCtrl, cmdKeys);
  233.             }
  234.         if (dItem == PrefStag) {
  235.             stag =!stag;
  236.             SetCtlValue((ControlHandle) stagHndl, stag);
  237.             }
  238.         if (dItem == PrefsysKCHR) {                                    /* BYU 2.4.18 */
  239.             sysKCHR =!sysKCHR;                                        /* BYU 2.4.18 */
  240.             SetCtlValue((ControlHandle) sysKCHRCtrl, sysKCHR);        /* BYU 2.4.18 */
  241.             }                                                        /* BYU 2.4.11 */
  242.         if (dItem == PrefBlink) {                                    /* BYU 2.4.11 */
  243.             blink =!blink;                                            /* BYU 2.4.11 */
  244.             SetCtlValue((ControlHandle) blinkCtrl, blink);            /* BYU 2.4.11 */
  245.             }                                                        /* BYU 2.4.11 */
  246.         if (dItem == PrefBlockCursor) {                                /* BYU 2.4.11 */
  247.             curs = 0;                                                /* BYU 2.4.11 */
  248.             SetCtlValue((ControlHandle) curs1Ctrl, 1);                /* BYU 2.4.11 */
  249.             SetCtlValue((ControlHandle) curs2Ctrl, 0);                /* BYU 2.4.11 */
  250.             SetCtlValue((ControlHandle) curs3Ctrl, 0);                /* BYU 2.4.11 */
  251.             }                                                        /* BYU 2.4.11 */
  252.         if (dItem == PrefUnderscoreCursor) {                        /* BYU 2.4.11 */
  253.             curs = 1;                                                /* BYU 2.4.11 */
  254.             SetCtlValue((ControlHandle) curs1Ctrl, 0);                /* BYU 2.4.11 */
  255.             SetCtlValue((ControlHandle) curs2Ctrl, 1);                /* BYU 2.4.11 */
  256.             SetCtlValue((ControlHandle) curs3Ctrl, 0);                /* BYU 2.4.11 */
  257.             }                                                        /* BYU 2.4.11 */
  258.         if (dItem == PrefVerticalCursor) {                            /* BYU 2.4.11 */
  259.             curs = 2;                                                /* BYU 2.4.11 */
  260.             SetCtlValue((ControlHandle) curs1Ctrl, 0);                /* BYU 2.4.11 */
  261.             SetCtlValue((ControlHandle) curs2Ctrl, 0);                /* BYU 2.4.11 */
  262.             SetCtlValue((ControlHandle) curs3Ctrl, 1);                /* BYU 2.4.11 */
  263.             }                                                        /* BYU 2.4.11 */
  264.  
  265.  
  266.         }
  267.  
  268.     if (dItem==DLOGCancel) {
  269.             DisposDialog( dtemp);
  270.             return;
  271.             }
  272.  
  273.     GetIText( CTtString, tempp);        /* BYU LSC */
  274.     p2cstr(tempp);                        /* BYU LSC */
  275.     sscanf((char *) tempp, "%d", &tempi);
  276.     Prefs.CTthresh  = tempi;
  277.  
  278.     Prefs.cmdKeys=cmdKeys;                    
  279.     Prefs.MacBinary=MacBinary;
  280.     Prefs.MacBReset=MacBReset;
  281.     Prefs.optKeys=optKeys;
  282.     Prefs.dontclose=dontclose;
  283.     Prefs.mapTilde =tildeMap;
  284.     Prefs.stag = stag;
  285.     Prefs.systemKCHR = sysKCHR;        /* BYU 2.4.18 */
  286.     Prefs.cursorBlink = blink;        /* BYU 2.4.11 */
  287.     Prefs.cursorType = curs;        /* BYU 2.4.11 */
  288.  
  289.     initKeyBoardMapping();            /* BYU 2.4.13 */
  290.  
  291.     if (theWorld.systemVersion < 0x0700) {    /* BYU 2.4.18 */
  292.         if (optKeys)
  293.             optionKeys();                    /* Turn it on.... */
  294.         else 
  295.             optionKeysOff();                /* Turn it off.... */
  296.     }                                        /* BYU 2.4.18 */
  297.  
  298.     mapTilde = tildeMap;            /* Change the real variable */
  299.     cursorBlink = blink;            /* BYU 2.4.11 */
  300.     cursorType = curs;                /* BYU 2.4.11 */
  301.  
  302.     switchMenus( cmdKeys);
  303.     whichMenus=cmdKeys;
  304.  
  305.     if (dItem == PrefSave)
  306.         SavePrefs();
  307.     DisposDialog( dtemp);
  308. }
  309.  
  310. /*
  311.  *    GetPrefs()    - gets the current preferences from the 'NPRF' 1 resource.
  312.  *            assumes RSRC file has already been opened.  Returns 0 if everything
  313.  *            is wonderful and 1 if the resource doesn't exist or isn't the right
  314.  *            version.
  315.  *
  316.  */
  317. int GetPrefs
  318.   (
  319.     void
  320.   )
  321. {
  322.     NCSAPrefs *ppref, **hpref;
  323.     
  324.     Prefs.cmdKeys = whichMenus;    /* when we have a blank file, override default with config.tel */
  325.  
  326.     if (SettingsFile == -1)
  327.         return(-1);                /* Can't get it if there is no settings file */
  328.  
  329.     UseResFile( SettingsFile);
  330.     hpref=(NCSAPrefs **)GetResource('NPRF',1);        /* Get the resource */
  331.  
  332.     if (hpref==0L)
  333.         return(1);                                    /* Doesn't exist */
  334.  
  335.     ppref = *hpref;                                    /* BYU 2.4.20 - Get the pointer */
  336.     if (ppref->Version < PREF_Version)                                /* BYU 2.4.18 */
  337.         SetHandleSize(hpref,(size_t) PrefLength[PREF_Version]);        /* BYU 2.4.18 */
  338.  
  339.     HLock((Handle) hpref);
  340.         if (ppref->Version < PREF_Version)
  341.             memmove( ppref, &Prefs,(size_t) PrefLength[ ppref->Version]);    /* BYU LSC - Coerce by using known lengths */
  342.         else     
  343.             Prefs = *ppref;                                            /* Get the data */
  344.  
  345.         Prefs.Version = PREF_Version;                    /* We are now current */
  346.     HUnlock((Handle) hpref);
  347.  
  348.     /* Set everything up that way.... */
  349.  
  350.     if (theWorld.systemVersion < 0x0700) {    /* BYU 2.4.18 */
  351.         if (Prefs.optKeys)
  352.             optionKeys();                    /* Turn it on.... */
  353.         else 
  354.             optionKeysOff();                /* Turn it off.... */
  355.     } else                                    /* BYU 2.4.19 */
  356.         Prefs.optKeys = 0;                    /* BYU 2.4.19 */
  357.  
  358.     switchMenus( Prefs.cmdKeys);
  359.     whichMenus=Prefs.cmdKeys;        /* Exported Variables */
  360.     MacBinary=Prefs.MacBinary;
  361.     mapTilde = Prefs.mapTilde;            /* Change the real variable */
  362.     cursorBlink = Prefs.cursorBlink;    /* BYU 2.4.11 */
  363.     cursorType = Prefs.cursorType;        /* BYU 2.4.11 */
  364.  
  365.     return(0);
  366. }
  367.